<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="行政编码" prop="adminCode">
      <el-input v-model="dataForm.adminCode" placeholder="行政编码"></el-input>
    </el-form-item>
    <el-form-item label="省级单位名称" prop="province">
      <el-input v-model="dataForm.province" placeholder="省级单位名称"></el-input>
    </el-form-item>
    <el-form-item label="市级单位名称" prop="prefectureName">
      <el-input v-model="dataForm.prefectureName" placeholder="市级单位名称"></el-input>
    </el-form-item>
    <el-form-item label="区县级单位名称" prop="cName">
      <el-input v-model="dataForm.cName" placeholder="区县级单位名称"></el-input>
    </el-form-item>
    <el-form-item label="电话区号" prop="areacode">
      <el-input v-model="dataForm.areacode" placeholder="电话区号"></el-input>
    </el-form-item>
    <el-form-item label="邮政编码" prop="zipCod">
      <el-input v-model="dataForm.zipCod" placeholder="邮政编码"></el-input>
    </el-form-item>
    <el-form-item label="拼音" prop="cPinyin">
      <el-input v-model="dataForm.cPinyin" placeholder="拼音"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          areacodeId: 0,
          adminCode: '',
          province: '',
          prefectureName: '',
          cName: '',
          areacode: '',
          zipCod: '',
          cPinyin: ''
        },
        dataRule: {
          adminCode: [
            { required: true, message: '行政编码不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省级单位名称不能为空', trigger: 'blur' }
          ],
          prefectureName: [
            { required: true, message: '市级单位名称不能为空', trigger: 'blur' }
          ],
          cName: [
            { required: true, message: '区县级单位名称不能为空', trigger: 'blur' }
          ],
          areacode: [
            { required: true, message: '电话区号不能为空', trigger: 'blur' }
          ],
          zipCod: [
            { required: true, message: '邮政编码不能为空', trigger: 'blur' }
          ],
          cPinyin: [
            { required: true, message: '拼音不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.areacodeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.areacodeId) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/areacode/info/${this.dataForm.areacodeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.adminCode = data.areacode.adminCode
                this.dataForm.province = data.areacode.province
                this.dataForm.prefectureName = data.areacode.prefectureName
                this.dataForm.cName = data.areacode.cName
                this.dataForm.areacode = data.areacode.areacode
                this.dataForm.zipCod = data.areacode.zipCod
                this.dataForm.cPinyin = data.areacode.cPinyin
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/areacode/${!this.dataForm.areacodeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'areacodeId': this.dataForm.areacodeId || undefined,
                'adminCode': this.dataForm.adminCode,
                'province': this.dataForm.province,
                'prefectureName': this.dataForm.prefectureName,
                'cName': this.dataForm.cName,
                'areacode': this.dataForm.areacode,
                'zipCod': this.dataForm.zipCod,
                'cPinyin': this.dataForm.cPinyin
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
